package com.yk.ykmessagesdk.utils;

import android.app.Application;
import android.util.Log;

import java.lang.reflect.Method;

public class ApplicationUtils {

    public static Application getApplication() {
        Application application = null;
        try {
            Method declaredMethod = Class.forName("android.app.ActivityThread").getDeclaredMethod("currentApplication", new Class[0]);
            declaredMethod.setAccessible(true);
            application = (Application) declaredMethod.invoke(null, new Object[0]);
            Log.d("ApplicationUtil", "curApp class1:" + application);
        } catch (Exception e) {
            Log.d("ApplicationUtil", "e:" + e.toString());
        }
        if (application != null) {
            return application;
        }
        try {
            Method declaredMethod2 = Class.forName("android.app.AppGlobals").getDeclaredMethod("getInitialApplication", new Class[0]);
            declaredMethod2.setAccessible(true);
            application = (Application) declaredMethod2.invoke(null, new Object[0]);
            Log.d("ApplicationUtil", "curApp class2:" + application);
        } catch (
                Exception e2) {
            Log.d("ApplicationUtil", "e:" + e2.toString());
        }
        return application;
    }


}
